package com.founder.devwork.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.founder.devwork.dto.AuthorStatsDTO;
import com.founder.devwork.dto.MonthlyStatDTO;
import com.founder.devwork.dto.ProjectSummaryDTO;
import com.founder.devwork.dto.RepositorySummaryDTO;
import com.founder.devwork.entity.Project;
import com.founder.devwork.entity.ProjectRepository;
import com.founder.devwork.service.ProjectRepositoryService;
import com.founder.devwork.service.ProjectService;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/projects")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ProjectRepositoryService projectRepositoryService;

    @GetMapping
    public Page<Project> getProjects(Page<Project> page) {
        page.setSearchCount(true);
        return projectService.page(page);
    }

    @GetMapping("/summaries")
    public IPage<ProjectSummaryDTO> getProjectSummaries(
        @RequestParam(defaultValue = "1") int page,
        @RequestParam(defaultValue = "10") int size,
        @RequestParam(required = false) String name,
        @RequestParam(required = false) String sortBy,
        @RequestParam(required = false, defaultValue = "DESC") String sortDir
    ) {
        Page<ProjectSummaryDTO> pageObj = new Page<>(page, size);
        return projectService.getProjectSummaries(pageObj, name, sortBy, sortDir);
    }

    @GetMapping("/{id}/repositories")
    public List<RepositorySummaryDTO> getRepositoryByProjectId(@PathVariable String id) {
        return projectService.getRepositoryByProjectId(id);
    }

    @GetMapping("/{id}/monthly-stats")
    public List<MonthlyStatDTO> getProjectMonthlyStats(@PathVariable String id) {
        return projectService.getProjectMonthlyStats(id);
    }

    @GetMapping("/{id}/author-stats")
    public List<AuthorStatsDTO> getProjectAuthorStats(@PathVariable String id) {
        return projectService.getProjectAuthorStats(id);
    }

    @PostMapping
    public Project addProject(@RequestBody Project project) {
        project.setCreatedAt(LocalDateTime.now());
        project.setUpdatedAt(LocalDateTime.now());
        projectService.save(project);
        return project;
    }

    @PostMapping("/{id}/repositories")
    public void addRepository(@PathVariable String id, @RequestBody Map<String, String> request) {
        String repositoryId = request.get("repositoryId");
        ProjectRepository assoc = new ProjectRepository();
        assoc.setProjectId(id);
        assoc.setRepositoryId(repositoryId);
        assoc.setCreatedAt(LocalDateTime.now());
        assoc.setUpdatedAt(LocalDateTime.now());
        projectRepositoryService.save(assoc);
    }

    @DeleteMapping("/repositories/{projectRepoId}")
    public void deleteRepository(@PathVariable String projectRepoId) {
        projectRepositoryService.removeById(projectRepoId);
    }

    @PutMapping("/{id}")
    public Project updateProject(@PathVariable String id, @RequestBody Project project) {
        project.setId(id);
        project.setUpdatedAt(LocalDateTime.now());
        projectService.updateById(project);
        return project;
    }

    @DeleteMapping("/{id}")
    public void deleteProject(@PathVariable String id) {
        projectService.removeProjWithRelationOfRepoByProjId(id);
    }
}
